     MODULE ALLOC

! this module includes all public variables

! grid size:

      integer :: &
     &  nx,ny,ns &
     &  ,nx1,ny1,ns1,nx2,ny2 &
     &  ,nxm1,nym1,nall,nxy,nxs,nys,jy,js,nall3,nallm,nallmm
      integer:: &
     &  isqv,isqc,isqr,nx1qv,ny1qv,ns1qv &
     &  ,nx1qc,ny1qc,ns1qc &
     &  ,nx1qr,ny1qr,ns1qr

!     Dima@ 02.05.07                                                           
!     variable for impurity switcher
      integer:: & 
      & impur 
!     flags indicating the immediate and constant impurity
      logical:: & 
     &  immediate_impur, constant_impur 
!     coordinates of the impurity signal point
      integer:: & 
     &  ix_impur, iy_impur 
!     impurity le
      real(kind(0.d0)),allocatable,dimension(:,:)::le_impur 
!     impurity value
      real(kind(0.0D0)) :: impur_value 

! u,v,w : wind components
! wsig:   d/dt (sigma) component of velocity in sigma coordinates
! us,vs: reference state wind
! dppref: reference state dp/dt
! psufref: reference state psuf

      real(kind(0.d0)),allocatable,dimension(:,:,:,:):: &
     &  u,v,w,wsig,pt

      real(kind(0.0d0)), allocatable :: dif000(:,:,:)
      real(kind(0.0d0)), allocatable :: dif100(:,:,:)
      real(kind(0.0d0)), allocatable :: dif101(:,:,:)
      real(kind(0.0d0)), allocatable :: dif010(:,:,:)
      real(kind(0.0d0)), allocatable :: dif011(:,:,:)
      real(kind(0.0d0)), allocatable :: dif001(:,:,:)
      real(kind(0.0d0)), allocatable :: dif110(:,:,:)

      real(kind(0.0D0)),allocatable,dimension(:,:,:,:):: &
      & qv,qc,qci,qr,qsn
      real(kind(0.0D0)),allocatable :: meth(:,:,:,:), meths(:,:,:)
      real(kind(0.0D0)),allocatable :: difunmeth(:,:,:)
      real(kind(0.0D0)),allocatable,dimension(:,:,:):: &
     &  difunq, qvs, cond, evap, difunqc, auto, col, &
     &  vini,imlt, hmfrz, sacrw, sacrwr, sbercw, ibercw, &
     &  smlt, sacrr, iacr, sberci, sagg, saci, raci, &
     &  vrain, difunqr, difunqci, difunqsn, &
     &  sumsources, meth_OH_oxidat, OHconc, rainw_subsid
      real(kind(0.0D0)),allocatable,dimension(:,:)::prec
      real(kind(0.0D0)),allocatable,dimension(:,:,:)::zeroarr

!     Dima@ 04.04.07
      real(kind(0.0D0)),allocatable,dimension(:,:,:,:)::qs_10
      real(kind(0.0D0)),allocatable,dimension(:,:,:)::qs_10s
      
!     Dima@ 13.04.07
      real(kind(0.0D0)),allocatable,dimension(:,:,:)::difunq_10 

      real(kind(0.d0)),allocatable,dimension(:,:,:):: &
     &  wk1,wk2,wk3,wk4,wk5,wk6,wk7,wk8,wk9

!     Dima@ 13.05.07     
      real(kind(0.d0)),allocatable,dimension(:,:,:)::  & 
     &  wk_impur 
      logical :: wk_impur_allocated  

      real(kind(0.d0)),allocatable,dimension(:,:,:):: &
     &  us,vs,pts,tems,difunu,difunv,difunw,difunt,phi,phis,s &
     &  ,thetav,difcoft
      real(kind(0.d0)),allocatable,dimension(:,:)::dppref,psufref
      real(kind(0.d0)),allocatable,dimension(:,:):: &
     &  tsuf,psufi,tsufi,phisui,deltaz,ptsoil,uvsuf,qvsoil,phisuf &
     &  ,phig,hk1,hk2,hk3,ppgra2,pplap,gpplap
      real(kind(0.d0)), allocatable :: methsoil(:,:)
      real(kind(0.d0)),allocatable:: &
      & psuf(:,:),pp(:,:,:),pp10(:,:,:),pp01(:,:,:),ppdx10(:,:,:) &
      & ,ppdx(:,:,:) &
      & ,dpdx10(:,:,:),ppdy01(:,:,:),ppdy(:,:,:),dpdy01(:,:,:) &
      & ,hsuf(:,:),dpp(:,:),hsufmax(:,:)
      real(kind(0.d0)),allocatable,dimension(:,:) :: &
      & uwind,vwind,q,potemp,zlevel

! boundary values of u,v,pp,pt - obtained by extrapolation (radiative
! boundary conditions)

      real(kind(0.d0)),allocatable:: &
      &  ubx(:,:,:),vbx(:,:,:) &
      & ,uby(:,:,:),vby(:,:,:) &
      & ,ucc(:,:,:),vcc(:,:,:) &
      & ,ubx2(:,:,:),vbx2(:,:,:) &
      & ,uby2(:,:,:),vby2(:,:,:) &
      & ,ucc2(:,:,:),vcc2(:,:,:) &
      & ,ppbx(:,:),ppby(:,:),ppcc(:,:) &
      & ,ppbx2(:,:),ppby2(:,:),ppcc2(:,:) &
      & ,ptbx(:,:,:),ptby(:,:,:),ptcc(:,:,:) &
      & ,qvbx(:,:,:),qvby(:,:,:),qvcc(:,:,:) &
      & ,qcbx(:,:,:),qcby(:,:,:),qccc(:,:,:) &
      & ,qcibx(:,:,:),qciby(:,:,:),qcicc(:,:,:) &
      & ,qrbx(:,:,:),qrby(:,:,:),qrcc(:,:,:) &
      & ,qsnbx(:,:,:),qsnby(:,:,:),qsncc(:,:,:) &
      & ,methbx(:,:,:),methby(:,:,:),methcc(:,:,:) &
      & ,pgradx(:,:,:),pgrads(:,:,:) &
      & ,pgrady(:,:,:)
    
      real(kind(0.d0)),allocatable:: & 
     & qscc(:,:,:),qsbx(:,:,:),qsby(:,:,:)  

! pt: perturbation potential temperature
! pts: reference state potential temperature
! tems: reference state temperature
! tsuf: temperature at the surface
! psuf: surface pressure
! ptop: pressure at the top of the model
! pp=psuf-ptop
! hsuf: height of the lower surface
!
! psufi,tsufi: psuf,tsuf in the initial state
!
! data for calculation of bulk transfer in the lowest hakf layer
!
! diffusion terms
!
! parameters for rayleigh relaxation in the absorption layer

      
      real(kind(0.d0)),dimension(:),allocatable:: &
     &  endrag,taudra,deltazt
      integer:: idrmax
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!
! phi: perturbation geopotential
! phis: reference state geopotential
! phisuf: phis at the surface
! phig:
!
! pgradx,pgrady,pgrads: boundary values of pp gradient (auxiliary var)
!
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!
! grid variables
!
      real(kind(0.d0)),dimension(:),allocatable:: &
     &  sigma0,sigma1 &
     &  ,ds0,ds02,ds04 &
     &  ,ds02a,ds04a,ds08a &
     &  ,ds1,ds12,ds14 &
     &  ,s1ds0,s0ds1,s0ds2a,s0ds4a &
     &  ,s1ds4a &
     &  ,ds1x,ds1dx,ds1y,ds1dy &
     &  ,dtxs,dtys,dxys

      real(kind(0.d0)) :: dtop, dsurf

      real(kind(0.d0)):: &
     &  dsng,dsr05 &
     &  ,dx2,dx4,dx8,dx05,dx025,dxx,dxdt2,dxt2 &
     &  ,dy2,dy4,dy8,dy05,dy025,dyy,dydt2,dyt2 &
     &  ,dxy,dxy025,dtxy &
     &  ,dtl

      integer:: nstep, nstep1
      integer :: nmonth_tot
      real(kind(0.d0)),allocatable:: delka2(:)

! fcor: coriolis parameter

      real(kind(0.d0)):: fcor


! utm coordinates

      real(kind(0.d0)) xminutm,xmaxutm,yminutm,ymaxutm

! parameters for fft

      integer::ifax(10),ifay(10)
      real(kind(0.d0)),allocatable::trigsx(:),trigsy(:) &
     &  ,sipcox(:),simcox(:),sipcoy(:),simcoy(:) &
     &  ,xlamb(:),ylamb(:),xylamb(:,:) &
     &  ,eigxy(:,:)

   
! work space
!
! note: wm2 needed from moment to wsigbc
! note: uflux,vflux,wflux needed from flux to moment next time step

      real(kind(0.d0)),allocatable:: &
     &  theta(:,:,:) &
     &  ,hkk1(:,:),hkk2(:,:) &
     &  ,hkk3(:,:),hkk4(:,:),hkk5(:,:) 
      integer,allocatable::ihkk1(:,:),ihkk2(:,:)

      real(kind(0.0d0)),allocatable:: &
     &  rh(:,:,:),ri(:,:,:) &
     &  ,work1(:,:,:),work2(:,:,:),work3(:,:,:),work4(:,:,:) &
     &  ,work5(:,:,:),work6(:,:,:),work7(:,:,:),work8(:,:,:)

      real(kind(0.d0)),dimension(:),allocatable:: w1d1,w1d2

      integer,allocatable::iworkk(:,:,:)

! qv - water vapour
! qc - cloud water (liquid)

       real(kind(0.d0))::qif,qdrag
       integer::ifqc,ifqr,ifqi,ifblob

! soil
! fields for solonoi
! also radiation and precipitation and geostrophic wind
! rainacc: accumulated precipitation during the run

      real(kind(0.d0)),allocatable,dimension(:,:):: &
     &  veg,xlai,rsmin,alb,emis,z0,z0h,rra,z0hw,z0w,rg,rat,xlake &
     &   ,pl,zonirr,tsmed,tslake,tsnoi,t2noi,wgnoi,w2noi,wrnoi &
     &   ,tsurw,tmsur,rn,h,le,gsolo,xdd2,cdm,rainacc,fmeth,xice &
     &   ,snowini,watdepth

      real(kind(0.d0)), allocatable :: fcveg(:,:,:)

      integer,dimension(:,:),allocatable:: &
     &  iclay,isand,iveg

      real(kind(0.d0)),allocatable:: &
     &  xpl(:),xra(:),xrg(:)

      integer:: &
     &   li,lf,nf,ndados,ifsoil,ifhle

      integer :: ifmeth, ifcmodel

      integer :: ecoclimap

      real(kind(0.d0)):: &
     &   ts(2),t2(2),wg(2),w2(2),wr(2),rhoa,z0hz0,timeini

      real(kind(0.d0)), allocatable :: rhoasurf(:,:)

      real(kind(0.d0)),allocatable,dimension(:,:):: &
     &  temmax,temmin,evapor,ptforc &
     &  ,phi00s

      real(kind(0.d0)),allocatable:: &
     &  ugeos(:),vgeos(:),xugeos(:,:),xvgeos(:,:)

      real(kind(0.d0)):: fcori

      real(kind(0.d0)),allocatable,dimension(:):: &
     &  tauspo,tauspoy

      real(kind(0.d0)), allocatable:: &
     &  a1phi(:),a2phi(:,:),a3phi(:),ctphi(:,:),dtphi(:,:) &
     &  ,sine(:),sines(:,:)

      real(kind(0.d0)),allocatable,dimension(:):: &
     &  a1pos3,a3pos3
      real(kind(0.d0)),allocatable,dimension(:,:):: &
     &  a21pos3

      real(kind(0.d0)),allocatable,dimension(:):: &
     &  aves

      real(kind(0.d0)), allocatable:: &
     &  errp(:,:)
      integer, allocatable:: &
     &  idath(:,:)

      real(kind(0.d0)),allocatable,dimension(:):: &
     &  duul,duur,dvvl,dvvr &
     &  ,phibx1,phiby1,phibx2,phiby2

      real(kind(0.d0)),allocatable:: &
     &  a1sphi(:),a2sphi(:),a3sphi(:),csphi(:),dsphi(:) &
     &  ,hk11(:,:),hk12(:,:)

      real(kind(0.d0)),allocatable :: dphsdx(:,:),dphsdy(:,:),hdamp(:)

      real(kind(0.d0)), allocatable :: &
     &  ff(:,:),tbot(:,:),dlnpdx(:,:),dlnpdy(:,:) &
     &  ,dlntdx(:,:),dlntdy(:,:) &
     &  ,dpsudx(:,:),dpsudy(:,:),hk21(:,:,:),hk22(:,:,:)

      real(kind(0.d0)) :: dragm, dragt
      real(kind(0.d0)), allocatable :: zlevm(:), zlev(:)
     
      integer(4) iflake,radpar,rat_par,nradcall, &
     & nlakecall,nsoilcall,shortwave,longwave 
      real(8) month,day,tau_rg 
      real(kind(0.d0)), dimension(:,:,:), allocatable:: & 
     & Radheat,Srad,Lrad  
      real(kind(0.d0)), dimension(:,:), allocatable:: &
     & Srad_surf,Lrad_surf, &
     & SraddirUV_surf, SraddirPAR_surf, SraddirIR_surf, &
     & SraddifUV_surf, SraddifPAR_surf, SraddifIR_surf
      real(kind(0.d0)), dimension(:,:), allocatable:: &
     & tsurf 
    
      integer imonth,iday,ihour,iminu,iseco 

      real(kind(0.d0)) :: timeho
      real(kind(0.d0)) :: t1_cpu,t2_cpu,t_exec(1:100) 


! options:
!
! hydro : if not.true. phi is obtained from the elliptic equation.
!
! iophis = 1(hydrostatic) 2(poisson eq dirichlet) 3(poisson eq neuman)
! ioppuv = 1(u v define boundary dpp/dt) 2(rad.b.c. for pp define u v)
! iobxxx =
! iowlbc = 0(lateral b.c. for w from wsig, and wsig b.c. from contin.)
!          1(lateral b.c. for wsig from w, and w from u and v b.c.)
      
      logical :: hydro,tfct,inipt,raylei,olddra,mxspan,adjust
      logical :: impldiff

      real(kind(0.d0)):: &
     &   dx,dy,dt,xl,yl,dt2 &
     &  ,xlcor,xrcor,ylcor,yrcor
      logical fluxxlcor,fluxylcor,fluxxrcor,fluxyrcor
      integer :: ntime
      integer :: ntot, kt, nrealiz
      integer :: nacc
      integer :: cartproj
      real(kind(0.d0)) :: mapsc

      integer :: &
     &  nupsur,nuppts,nupdif &
     &  ,iophis,ioppuv,iowlbc &
     &  ,iodify,iobphy,iobuux,iobuuy,iobvvx,iobvvy,iobptx,iobpty &
     &  ,iobppx,iobppy

      real(8) :: radbch3d, radbch1d

! mountain: iomtyp:1- 2d mountain; 2- 3d mountain
! ivarhs=0 mountain height is established at nstep=0 and is fixed
! ivarhs=1 mountain height grows linearly from nstep=0 to nstepgrow and stays fixed afterwards
! ivarhs=2 mountain height grows in steps

      integer :: &
     &  iomtyp,ivarhs,nstepgrow,nstepflat,numsteps
      real(kind(0.d0)):: &
     &  hmount,xmount,ymount,xspan,yspan,hxwid,hywid,hexp &
     &  ,xmountc,ymountc

! aselin time filter

      real(kind(0.d0)) :: filt,ppfilt


      real(kind(0.d0)):: r,cp,g,hlat,p00,rv,hlatcp,xk4,qco,xnor,rhol &
     &  ,xk1,dv,xkt,xniu,rho0,xa,xb,sch,gm48,gm38,gm29,akapa,gr,g2,r05 &
     &  ,gr2,g4,g05,omega,eps,ttmin,stefan,hsub,hfus,hsubcp,hfuscp,rearth &
     &  ,row,Kelvin0 
      parameter(r=287.,rstar=8.314,cp=1005.,g=9.8066, &
     & hlat=2.501e6,hsub=2.837e6,hfus=3.336e5,p00=1.e5)
      parameter(rv=461.51,hlatcp=hlat/cp,hsubcp=hsub/cp,hfuscp=hfus/cp, &
     & xk4=1.e-3,qco=1.e-3,xnor=1.e7,rvrm1=rv/r-1.)
      parameter(rhol=1.e3,xk1=1.506,dv=2.26e-5,xkt=2.43e-2 &
     & ,xniu=1.51e-5,rho0=1.23,xa=842.,xb=0.8,sch=xniu/dv &
     & ,gm48=17.837870,gm38=4.694174,gm29=1.827355)

!     note: ha is the height corresponding to the surface pressure pa.
      
      parameter(akapa=r/cp,gr=g/r,g2=2.*g,r05=0.5*r,gr2=gr*2.,g4=4.*g)
      parameter(g05=0.5*g)
      parameter(omega=7.292e-5)
      parameter(eps=1.e-6,ttmin=1.e-5)
      parameter(stefan = 5.6697e-08)
      parameter(rearth = 6.378d+6) ! Earth radius
      parameter(row = 1.d+3) ! Reference water density , kg/m**3
      parameter(Kelvin0 = 273.15d0)


      real(kind(0.d0)):: xlatit,xlongit,ptop,pa,ha
      common/par301/xlatit
      common/par302/ptop,pa,ha

!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!
! iodif = 1(constant dif.coef.) 2(deformation dependent)
!

      logical :: dohsmo,dovsmo,perdam,dovsmt
      real(kind(0.d0)) :: hdampm,hdampt,vdamp,vdampt
      integer :: numsmo

      real(kind(0.d0)):: &
     & rikey,difcof,uvdif,tsdif,difl
      integer :: &
     &  iodif &
     &  ,nxsponge1,nxsponge2,nysponge1,nysponge2 &
     &  ,ntaut,ntauspon
      real(kind(0.d0)) :: &
     &  cdcoef,taum,taut,zm,zt,tauspon

      real(kind(0.d0)), parameter :: qsdif = 0.d0, tdif = 1.d0, qdif = 1.d0
      real(kind(0.d0)), parameter :: dkapa = 0.8d0

      real(kind(0.d0)) :: thresh_cutneg

!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!     aux
 
      real*8 hh, declive, planalts, planalti

!
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

      integer npro
      real(kind(0.d0)) &
     &   t0ini,ug,vg,dugdz,dvgdz,psref,dzpro

      real(kind(0.d0)),allocatable, dimension(:):: &
     &   zpro,fpro,fnpro,press0,press1,dpressdt
      real(kind(0.d0)),allocatable, dimension(:,:) :: &
     &   thpro0,dthdz,dthdt,dthdzdt &
     &   ,uspro0,dusdz,dusdt,dusdzdt &
     &   ,vspro0,dvsdz,dvsdt,dvsdzdt &
     &   ,qvspro0,dqvsdz,dqvsdt,dqvsdzdt &
     &   ,thpro1,dthdz1,uspro1,dusdz1 &
     &   ,vspro1,dvsdz1,qvspro1,dqvsdz1 &
     &   ,tepro,presspro,rhopro,rhopro1

! ndat is the maximum number of points in the profile given
! ndat=max(ndth,ndus,ndvs,ndqvs)

      integer ndat

      real(kind(0.d0)),allocatable,dimension(:,:) :: &
     &   thdat,usdat,vsdat,qvsdat &
     &   ,zthdat,zusdat,zvsdat,zqvsdat,psdat,ptdat,pressdat &
     &   ,tedat

      character*8:: exptit
      character*50:: comlin(3)
      logical:: itepsuf

      integer :: &
      &  ioreft,iorefq,iorefu,iorefv,ndth,ndus,ndvs,ndqvs
      real(kind(0.d0)):: pts0

      real(kind(0.d0)) :: ug0,dugdz0,vg0,dvgdz0
      common/dat04/ug0,dugdz0,vg0,dvgdz0,itepsuf

!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!
! input/output control:
! iotfor: period of formatted output (unit nchn1);
! iotunf: period of unformatted output (unit 31, with initial formatted
! data in unit 30);
! iotrst: period of output of data for restart (units 40+).
!
! restar: if .true. the model starts from data of a previous run
! which must be provided on unit 21 (transfer to file ft21):
! activate corresponding card !!!                                       #exp#

      logical restar

! if rstout is true then:
! restart data output every iotrst time steps and at ntime.

      logical rstout

! forout: if true there is formatted output each iotfor time steps
! and at nstep=ntime.

      logical forout

! grdout if true the formatted output will include a grid file

      logical grdout

! unfout: if true there is unformatted output each iotunf time steps
! and at nstep=ntime. this output, on unit 30,31 must be transfered
! for a permament disk file, after being packed.
! activate corresponding cards !!!

      logical unfout

! output of wave drag:

      logical drgprt,domom

! choice of formatted output (see out6):

      logical norout,morout,refout,fluout,difout,surout &
      &   ,iniout,masout,driout,ddiout,defout,eldout,elfout,elgout &
      &   ,phsout,desout,echeck,eneout,ppbout,uubout,ptbout &
      &   ,duvout,inrout,iniunf,proout

! unit for formatted output:

      parameter(nchn1=28)

! verbose: -1(no screen output), 0(a few), 1(much), 2(all

      integer verbose

      logical prt,unfprt
      real(kind(0.d0)) :: dzlev
      integer:: &
     &  iotrst,iotfor,iotunf,iotdrg,iotduv,nlev,iomod &
     &  ,iostart,ioend,iodelta,iotscrout,iotpnt


! local output
 
      integer noutu
      integer,allocatable,dimension(:)::ixoutu,iyoutu,isoutu

! profile output

      integer noutp
      integer,allocatable,dimension(:):: ixoutp,iyoutp

!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

! parameters for radiative boundary conditions:

      real(kind(0.d0)):: rdx,rdy,rdt,dxdt,dydt,rxmax,rymax,rzmax
      common/radpar/rdx,rdy,rdt,dxdt,dydt,rxmax,rymax
      real(kind(0.d0)):: pi,pi05
      common /consta/ pi,pi05

! old nh3dprec.inc

      real(kind(0.d0)):: zero0,resmax
      parameter (zero0=1.d-30,resmax=1.d-12)

      integer::istep,kstep
      real(kind(0.d0)),dimension(:,:),allocatable :: pequiv


! work variables for inpos3c/pos3c

      real(kind(0.d0)),allocatable::emo(:,:),emi(:,:),vlam(:),e(:)


! variables for out6

      real(kind(0.d0)),allocatable,dimension(:,:)::panom,pini

      integer ifluxcor
      real(kind(0.d0)) dpmeddt

      END MODULE ALLOC
